
# 防止 IP 被封
# 提升访问速度
# 隐藏 ip

import urllib.request

url = 'https://www.baidu.com/s?wd=ip'

headers = {
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
}

request = urllib.request.Request(url=url,headers=headers)

# 代理 proxies
# https://www.kuaidaili.com/
proxies = {
    'http': '117.42.94.118:23012',
}
getProxy = urllib.request.ProxyHandler(proxies=proxies)

getopener = urllib.request.build_opener(getProxy)


# 调用 open
response = getopener.open(request)

content = response.read().decode('utf-8')

print(content)

f = open('../download/location.html','w',encoding='utf-8')

f.write(content)

f.close()

# 代理池
# import random
# proxies_pool = [
#     {'http':'192.122.22.1:9029'},
#     {'http':'192.122.21.1:9019'},
#     {'http':'192.122.23.1:9029'},
#     {'http':'192.122.24.1:9289'}
# ]
# proxies = random.choice(proxies_pool)
